package com.wish233.wiliwilivideo.mq;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wish233.wiliwiliapi.client.UserClient;
import com.wish233.wiliwiliapi.domain.vo.VideoChange;
import com.wish233.wiliwiliapi.domain.vo.VideoVO;
import com.wish233.wiliwilivideo.domain.dto.comment.CommentDeleteMessage;
import com.wish233.wiliwilivideo.domain.dto.comment.CommentSelectMessage;
import com.wish233.wiliwilivideo.domain.dto.likes.LikeCountMessage;
import com.wish233.wiliwilivideo.domain.dto.likes.LikeLocal2Redis;
import com.wish233.wiliwilivideo.domain.dto.likes.LikeOperationMessage;
import com.wish233.wiliwilivideo.domain.po.Comment;
import com.wish233.wiliwilivideo.domain.po.VideoViewHistory;
import com.wish233.wiliwilivideo.domain.vo.CommentVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.wish233.wiliwilivideo.domain.constant.AMQPConstant.*;

/**
 * @author WHH
 */
@Component
public class MqProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private UserClient userClient;

    // 发送点赞操作消息
    public void sendLikeOperationMessage(String operationType, String userId, Integer bizId, Long messageId) {
        LikeOperationMessage message = new LikeOperationMessage();
        message.setOperationType(operationType);
        message.setUserId(Long.valueOf(userId));
        message.setBizId(bizId);
        message.setMessageId(messageId);
        //设置消息的唯一ID
        message.setId(IdUtil.simpleUUID());
        rabbitTemplate.convertAndSend(LIKE_OPERATION_EXCHANGE, LIKE_OPERATION_ROUTING_KEY, message);
    }

    // 发送点赞计数消息
    public void sendLikeCountMessage(Integer bizId, Long messageId, Long delta) {
        LikeCountMessage message = new LikeCountMessage();
        message.setBizId(bizId);
        message.setMessageId(messageId);
        message.setDelta(delta);
        rabbitTemplate.convertAndSend(LIKE_COUNT_EXCHANGE, LIKE_COUNT_ROUTING_KEY, message);
    }

    //发送评论增加消息
    public void sendCommentMessage(List<CommentVO> commentVOS) {
        //如果只是删除只需要id
        CommentSelectMessage message = new CommentSelectMessage();
        message.setCommentVOS(commentVOS);
        message.setVideoId(commentVOS.get(0).getVideoId());
        message.setRootComment(commentVOS.get(0).getRootCommentId() == null);
        rabbitTemplate.convertAndSend(COMMENT_CACHE_UPDATE_EXCHANGE, COMMENT_CACHE_UPDATE_ROUTING_KEY, message);
    }

    //发送评论增加消息
    public void sendDeleteComment(Comment comment) {
        CommentDeleteMessage message = new CommentDeleteMessage();
        message.setCommentVO(toCommentVO(comment));
        message.setVideoId(comment.getVideoId());
        message.setRootComment(comment.getRootCommentId() == null);
        message.setToCommentId(comment.getToCommentId());
        rabbitTemplate.convertAndSend(COMMENT_CACHE_DELETE_EXCHANGE, COMMENT_CACHE_DELETE_ROUTING_KEY, message);
    }

    //发送评论增加消息
    public void sendCommentMessage(Comment comment) {
        //如果只是删除只需要id
        CommentSelectMessage message = new CommentSelectMessage();
        List<CommentVO> commentVOS = new ArrayList<>();
        commentVOS.add(toCommentVO(comment));
        message.setCommentVOS(commentVOS);
        message.setVideoId(comment.getVideoId());
        message.setRootComment(comment.getRootCommentId() == null);
        rabbitTemplate.convertAndSend(COMMENT_CACHE_UPDATE_EXCHANGE, COMMENT_CACHE_UPDATE_ROUTING_KEY, message);
    }

    public void sendHistoryMessage(VideoViewHistory videoViewHistory) {
        rabbitTemplate.convertAndSend(HISTORY_UPSERT_EXCHANGE, HISTORY_UPSERT_ROUTING_KEY, videoViewHistory);
    }

    public void send2ES(Object video, Integer changeType) {
        VideoChange change = new VideoChange();
        if (changeType == 1) {
            VideoVO videoVO = BeanUtil.copyProperties(video, VideoVO.class);
            change.setEntity(videoVO);
        } else {
            change.setEntity(video);
        }
        change.setChangeType(changeType);
        rabbitTemplate.convertAndSend("sync.es.mysql.exchange", "sync.es.mysql", change);
    }


    private CommentVO toCommentVO(Comment comment) {
        CommentVO commentVO = BeanUtil.copyProperties(comment, CommentVO.class);
        //填充发布者信息
        commentVO.setAuthor(userClient.getUserDTOById(comment.getPubId()));
        if (comment.getRootCommentId() == null) {
            return commentVO;
        }
        //如果不是根评论 就要查出回复的用户
        Long toCommentId = comment.getToCommentId();
        Comment to = Db.getById(toCommentId, Comment.class);
        commentVO.setReplyToUser(userClient.getUserDTOById(to.getPubId()));
        return commentVO;
    }

    public void sendLike2Mq4Redis(boolean isLiked,
                                  String userId, Integer bizId, Long messageId) {
        LikeLocal2Redis message = new LikeLocal2Redis();
        message.setUserId(userId);
        message.setBizId(bizId);
        message.setMessageId(messageId);
        message.setLiked(isLiked);
        rabbitTemplate.convertAndSend(LIKE_SYNC_REDIS_EXCHANGE, LIKE_SYNC_REDIS_ROUTING_KEY, message);
    }
}
